package com.xindaoapp.happypet;

import java.io.File;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.util.LogUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.xindaoapp.happypet.activity.FansActivity;
import com.xindaoapp.happypet.activity.FindPasswordActivity;
import com.xindaoapp.happypet.activity.MainActivity;
import com.xindaoapp.happypet.activity.MenuAttention;
import com.xindaoapp.happypet.activity.MessageListActivity;
import com.xindaoapp.happypet.activity.MyLoveActivity;
import com.xindaoapp.happypet.activity.MyOrderListActivity;
import com.xindaoapp.happypet.activity.MyShopCollectActivity;
import com.xindaoapp.happypet.activity.RegisterActivity;
import com.xindaoapp.happypet.activity.SettingActivity;
import com.xindaoapp.happypet.activity.UserInfoActivity;
import com.xindaoapp.happypet.api.ConstantsValue;
import com.xindaoapp.happypet.fragments.ConsigneeFragment;
import com.xindaoapp.happypet.fragments.LoginFragment;
import com.xindaoapp.happypet.fragments.MessageCenterFragment;
import com.xindaoapp.happypet.utils.AppVersionUtil;
import com.xindaoapp.happypet.utils.BitmapHelp;
import com.xindaoapp.happypet.utils.CommonParameter.UserState;
import com.xindaoapp.happypet.utils.CommonUtil;
import com.xindaoapp.happypet.utils.Constants;
import com.xindaoapp.happypet.utils.CustomToast;
import com.xindaoapp.happypet.view.CircleImageView;

public class RightMenuFragement extends Fragment implements OnClickListener {
    private Context context;
    @ViewInject(R.id.right_menu_login)
    private LinearLayout layout_right_menu_login;
    @ViewInject(R.id.right_menu_unlogin)
    private LinearLayout layout_right_menu_unlogin;
    @ViewInject(R.id.menu_login)
    private ImageButton menu_login;
    @ViewInject(R.id.menu_register)
    private TextView menu_register;
    @ViewInject(R.id.menu_find_psd)
    private TextView menu_find_psd;
    @ViewInject(R.id.menu_version)
    private TextView menu_version;
    @ViewInject(R.id.setting)
    private RelativeLayout setting;
    @ViewInject(R.id.menu_mylove)
    private RelativeLayout menu_mylove;
    @ViewInject(R.id.menu_myhf)
    private RelativeLayout menu_myhf;
    @ViewInject(R.id.menu_mysc)
    private RelativeLayout menu_mysc;
    @ViewInject(R.id.menu_mymsg)
    private RelativeLayout menu_mymsg;
    // 用户信息
    @ViewInject(R.id.menu_userinfo)
    private ImageButton menu_userinfo;
    @ViewInject(R.id.menu_userpic)
    private CircleImageView menu_userpic;
    @ViewInject(R.id.menu_username)
    private TextView menu_username;

    @ViewInject(R.id.right_menu_myclub)
    private RelativeLayout right_menu_myclub;
    @ViewInject(R.id.right_menu_myclub_content)
    private LinearLayout right_menu_myclub_content;
    @ViewInject(R.id.right_menu_myclub_image)
    private ImageView right_menu_myclub_image;

    // 我的商城
    @ViewInject(R.id.right_menu_myshop)
    private RelativeLayout right_menu_myshop;
    @ViewInject(R.id.right_menu_myshop_image)
    private ImageView right_menu_myshop_image;

    @ViewInject(R.id.right_menu_myshop_content)
    private LinearLayout right_menu_myshop_content;
    @ViewInject(R.id.menu_myorder)
    private RelativeLayout menu_myorder;
    @ViewInject(R.id.menu_mycollect)
    private RelativeLayout menu_mycollect;
    @ViewInject(R.id.menu_myaddress)
    private RelativeLayout menu_myaddress;
    @ViewInject(R.id.right_menu_message_center)
    private RelativeLayout right_menu_message_center;

    private SharedPreferences sp;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.rightmenu, null);
        ViewUtils.inject(this, view);
        return view;
    }

    @SuppressWarnings("static-access")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        context = getActivity();
        sp = context.getSharedPreferences("userconfig", context.MODE_PRIVATE);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        initRightMenu(UserState.isLogged());
        setClick();
        /**
         * 初始化version
         */
        menu_version.setText(AppVersionUtil.getVersion(context));
        initRightMenu();
        initBitMapUtils();
        super.onActivityCreated(savedInstanceState);
    }

    private void initRightMenu() {
        right_menu_myclub_image.setBackgroundResource(R.drawable.xiala_ok);
        right_menu_myclub_content.setVisibility(View.VISIBLE);
        right_menu_myclub.setBackgroundResource(R.drawable.common_menu_selector_bg);
    }

    private void setClick() {
        menu_login.setOnClickListener(this);
        menu_register.setOnClickListener(this);
        menu_find_psd.setOnClickListener(this);
        setting.setOnClickListener(this);
        menu_mylove.setOnClickListener(this);
        menu_userinfo.setOnClickListener(this);
        menu_myhf.setOnClickListener(this);
        menu_mysc.setOnClickListener(this);
        menu_mymsg.setOnClickListener(this);
        right_menu_myclub.setOnClickListener(this);

        right_menu_myshop.setOnClickListener(this);
        menu_myorder.setOnClickListener(this);
        menu_mycollect.setOnClickListener(this);
        menu_myaddress.setOnClickListener(this);
        right_menu_message_center.setOnClickListener(this);

    }

    private void initRightMenu(boolean isLogin) {
        if (isLogin) {
            layout_right_menu_unlogin.setVisibility(View.GONE);
            layout_right_menu_login.setVisibility(View.VISIBLE);
        } else {
            layout_right_menu_unlogin.setVisibility(View.VISIBLE);
            layout_right_menu_login.setVisibility(View.GONE);
        }
    }

    @Override
    public void onResume() {
        initRightMenu(UserState.isLogged());
        initUserTouxiangAndName(UserState.isLogged());
        super.onResume();
    }

    /**
     * 初始化用户的头像和名字
     */
    private void initUserTouxiangAndName(boolean isLogin) {
        if (isLogin) {
            menu_username.setText(UserState.getUser().username);
            if (ConstantsValue.ISUPLOADSUCCESS) {
                if (!TextUtils.isEmpty(sp.getString("uid", ""))) {
                    File filepath = new File(Environment.getExternalStorageDirectory(), "temp");
                    File file = new File(filepath, "userpic.jpg");
                    Bitmap decodeFile = BitmapFactory.decodeFile(file.getAbsolutePath());
                    menu_userpic.setImageBitmap(decodeFile);
                }
            } else {
                BitmapHelp.getBitmapUtils(context).display(menu_userpic,
                        UserState.getUser().userface, config);
            }
        }
    }

    private BitmapDisplayConfig config;

    private BitmapDisplayConfig initBitMapUtils() {
        config = new BitmapDisplayConfig();
        config.setLoadingDrawable(getResources().getDrawable(R.drawable.person_loading));
        config.setLoadFailedDrawable(getResources().getDrawable(R.drawable.person_loading));
        return config;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.menu_login:
                Intent loginA = new Intent(context, MainActivity.class);
                loginA.putExtra(Constants.KEY_SKIP_CLASS_NAME,
                        LoginFragment.class.getCanonicalName());
                startActivity(loginA);
                break;
            case R.id.menu_register:
                Intent registerA = new Intent(context, RegisterActivity.class);
                startActivity(registerA);
                break;
            case R.id.menu_find_psd:
                Intent findPswA = new Intent(context, FindPasswordActivity.class);
                startActivity(findPswA);
                break;
            case R.id.setting:
                Intent settingA = new Intent(context, SettingActivity.class);
                startActivity(settingA);
                break;
            case R.id.menu_mylove:
                Intent myloveA = new Intent(context, MyLoveActivity.class);
                startActivity(myloveA);
                break;
            case R.id.menu_userinfo:
                Intent userInfoA = new Intent(context, UserInfoActivity.class);
                startActivity(userInfoA);
                break;
            case R.id.menu_myhf:
                Intent myhfA = new Intent(context, MenuAttention.class);
                startActivity(myhfA);
                break;
            case R.id.menu_mysc:
                Intent myscA = new Intent(context, FansActivity.class);
                startActivity(myscA);
                break;
            case R.id.menu_mymsg:
                Intent mymsgA = new Intent(context, MessageListActivity.class);
                startActivity(mymsgA);
                break;
            case R.id.right_menu_myclub:
                int visibility = right_menu_myclub_content.getVisibility();
                if (visibility == View.GONE) {
                    right_menu_myclub_image.setBackgroundResource(R.drawable.xiala_ok);
                    right_menu_myclub_content.setVisibility(View.VISIBLE);
                    right_menu_myclub.setBackgroundResource(R.drawable.common_menu_selector_bg);
                } else {
                    right_menu_myclub_image.setBackgroundResource(R.drawable.xiala_false);
                    right_menu_myclub.setBackgroundResource(R.color.transparent);
                    right_menu_myclub_content.setVisibility(View.GONE);
                }

                right_menu_myshop_image.setBackgroundResource(R.drawable.xiala_false);
                right_menu_myshop.setBackgroundResource(R.color.transparent);
                right_menu_myshop_content.setVisibility(View.GONE);
                break;
            case R.id.right_menu_myshop:
                if (right_menu_myshop_content.getVisibility() == View.GONE) {
                    right_menu_myshop_image.setBackgroundResource(R.drawable.xiala_ok);
                    right_menu_myshop_content.setVisibility(View.VISIBLE);
                    right_menu_myshop.setBackgroundResource(R.drawable.common_menu_selector_bg);
                } else {
                    right_menu_myshop_image.setBackgroundResource(R.drawable.xiala_false);
                    right_menu_myshop.setBackgroundResource(R.color.transparent);
                    right_menu_myshop_content.setVisibility(View.GONE);
                }

                right_menu_myclub_image.setBackgroundResource(R.drawable.xiala_false);
                right_menu_myclub.setBackgroundResource(R.color.transparent);
                right_menu_myclub_content.setVisibility(View.GONE);
                break;
            case R.id.menu_myorder:
                // Intent mOrder = new Intent(context, ProductDetailActivity.class);
                Intent mOrder = new Intent(context, MyOrderListActivity.class);
                startActivity(mOrder);
                break;
            case R.id.menu_mycollect:
                Intent mCollect = new Intent(context, MyShopCollectActivity.class);
                startActivity(mCollect);
                break;
            case R.id.menu_myaddress:
                Intent mainIntent = new Intent(context, MainActivity.class);
                mainIntent.putExtra(Constants.KEY_IS_BACK, false);
                mainIntent.putExtra(Constants.KEY_SKIP_CLASS_NAME,
                        ConsigneeFragment.class.getSimpleName());
                startActivity(mainIntent);
                break;
            case R.id.right_menu_message_center:
                Intent messageCenterIntent = new Intent(context, MainActivity.class);
                messageCenterIntent.putExtra(Constants.KEY_SKIP_CLASS_NAME,
                        MessageCenterFragment.class.getCanonicalName());
                startActivity(messageCenterIntent);
                break;
            default:
                break;
        }
    }

    public void loadData(HttpRequest.HttpMethod method, String url, RequestParams params,
            RequestCallBack<String> callback) {
        HttpUtils http = new HttpUtils();
        http.configCurrentHttpCacheExpiry(1000 * 1);
        LogUtils.allowD = true;
        if (params != null) {
            if (params.getQueryStringParams() != null)
                LogUtils.d(url + params.getQueryStringParams().toString());
        } else {
            params = new RequestParams();
        }
        if (0 == CommonUtil.isNetworkAvailable(context)) {
            showToast("无网络，请检查网络连接！");
        } else {
            http.send(method, url, params, callback);
        }
    }

    public void showToast(String msg) {
        showToast(msg, 0);
    }

    public void showToast(String msg, int time) {
        CustomToast customToast = new CustomToast(context, msg, time);
        customToast.show();
    }
}
